<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="../css/elementui.css">
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }

        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/vue.js"></script>
    <script src="../js/axios.js"></script>
    <script src="../js/elementui.js"></script>


</head>
<body>
<div id="app">
    <el-form :inline="true">
        <el-form-item label="品牌名称">
            <el-input v-model="map.name" placeholder="品牌名称"></el-input>
        </el-form-item>
        <el-form-item label="品牌首字母">
            <el-input v-model="map.letter" placeholder="品牌名称"></el-input>
        </el-form-item>

        <el-button type="primary" @click="fetch">查询</el-button>
        <el-button type="primary" @click="pojo={},dialogVisible = true">新增</el-button>
    </el-form>
    <el-dialog title="品牌编辑" :visible.sync="dialogVisible">
        <el-form label-width="80px">
            <el-form-item label="品牌名称">
                <el-input placeholder="品牌名称" v-model="pojo.name"></el-input>
            </el-form-item>
            <el-form-item label="品牌首字母">
                <el-input placeholder="品牌首字母" v-model="pojo.letter"></el-input>
            </el-form-item>
            <el-form-item label="品牌图片">
                <el-upload
                        class="avatar-uploader"
                        action="/web/upload/oss.do?folder=brand"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="品牌排序">
                <el-input placeholder="品牌排序" v-model="pojo.seq"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="save()">保存</el-button>
                <el-button @click="dialogVisible = false">关闭</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>
    <el-table
            :data="tableData"
            border
            align="center"
            style="width: 100%">
        <el-table-column
                align="center"
                prop="id"
                label="ID"
                width="180">
        </el-table-column>
        <el-table-column
                align="center"
                prop="name"
                label="品牌"
                width="180">
        </el-table-column>
        <el-table-column
                align="center"
                label="图片"
                width="180">
            <template slot-scope="scope">
                <img width="100px" height="50px" :src="scope.row.image">
            </template>
        </el-table-column>
        <el-table-column
                align="center"
                prop="letter"
                label="首字母"
                width="180">
        </el-table-column>
        <el-table-column
                align="center"
                label="功能">
            <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" circle @click="update(scope.row.id)"></el-button>
                <el-button type="danger" icon="el-icon-delete" circle @click="del(scope.row.id)"></el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination
            @size-change="fetch"
            @current-change="fetch"
            :current-page.sync="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size.sync="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>

</div>

</body>
<script>
    new Vue({
        el: "#app",
        data: {
            tableData: [],
            currentPage: 1,
            pagesize:10,
            total:10,
            map: {},
            pojo: {},
            dialogVisible: false,
            imageUrl:''
        },
        methods: {
            fetch() {
                axios.post(`/web/brand/findpage.do?startpage=${this.currentPage}&size=${this.pagesize}`, this.map).then(response => {
                    this.tableData = response.data.rows;
                    this.total = response.data.total;
                }).catch(function (err) {
                    console.log(err);
                });
            },
            save() {
                this.pojo.image = this.imageUrl;
                axios.post(`/web/brand/${this.pojo.id == null ? `add` : `update`}.do?`, this.pojo).then(response => {
                    this.dialogVisible = false;
                    this.fetch();
                }).catch(function (err) {
                    console.log(err);
                });
            },
            update(id) {
                this.dialogVisible = true;
                axios.get(`/web/brand/findId.do?id=${id}`).then(response => {
                    this.pojo = response.data;
                    this.imageUrl = this.pojo.image;
                }).catch(function (err) {
                    console.log(err);
                });
            },
            del(id) {
                this.$confirm('此操作将永久删除id是'+id+'的文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get(`/web/brand/delete.do?id=${id}`).then(response => {
                        this.pojo = response.data;
                        this.fetch();
                    }).catch(function (err) {
                        console.log(err);
                    });
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleAvatarSuccess(res, file) {
                this.imageUrl = file.response;
            },

            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        },

        created() {
            this.fetch();
        }

    })


</script>
</html>